package com.zhiyou100.util;

import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.Properties;

public class PublicUtil {
    //邮件发送
    // 发件人 账号和密码
    public static final String MY_EMAIL_ACCOUNT = "miaotianbao@163.com";
    //public static final String MY_EMAIL_PASSWORD = "FKOSZAKLMKDLTUGG";// 密码,是你自己的设置的授权码
    public static final String MY_EMAIL_PASSWORD = "OXPWJHDNSTYHRNEW";
    // SMTP服务器(这里用的163 SMTP服务器)
    public static final String MEAIL_163_SMTP_HOST = "smtp.163.com";
    public static final String SMTP_163_PORT = "25";// 端口号,这个是163使用到的;QQ的应该是465或者875

    //发送邮件方法：参数：接收方、邮箱标题 邮箱内容
    public static void sendMessage(String receiver,String subject,String content)throws Exception {
        //属性集
        Properties p = new Properties();
        p.setProperty("mail.smtp.host", MEAIL_163_SMTP_HOST);
        p.setProperty("mail.smtp.port", SMTP_163_PORT);
        p.setProperty("mail.smtp.socketFactory.port", SMTP_163_PORT);
        p.setProperty("mail.smtp.auth", "true");//是否需要认证
        p.setProperty("mail.smtp.socketFactory.class", "SSL_FACTORY");
        //获取连接
        Session session = Session.getInstance(p, new Authenticator() {
            // 设置认证账户信息
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(MY_EMAIL_ACCOUNT, MY_EMAIL_PASSWORD);
            }
        });
        session.setDebug(true);//设置debug测试
        System.out.println("创建邮件");
        MimeMessage message = new MimeMessage(session);
        // 发件人
        message.setFrom(new InternetAddress(MY_EMAIL_ACCOUNT));
        // 收件人和抄送人
        message.setRecipients(Message.RecipientType.TO, receiver);
        // 内容(这个内容还不能乱写,有可能会被SMTP拒绝掉;多试几次吧)
        message.setSubject(subject);
        message.setContent(content, "text/html;charset=UTF-8");
        message.setSentDate(new Date());
        message.saveChanges();//保存设置
        System.out.println("准备发送");
        Transport.send(message);
        System.out.println("发送成功！");
    }

    //验证码
    public static void  getYZM(String  yzm,OutputStream out)throws Exception{
        //1 准备画板
        BufferedImage bin=
                new BufferedImage(170,40,BufferedImage.TYPE_INT_BGR);
        //2 准备画笔
        Graphics2D g=(Graphics2D)bin.getGraphics();
        // 3选背景颜色  白色
        g.setColor(Color.WHITE);
        //4 画背景
        g.fillRect(1,1,168,38);
        //5 选字体颜色
        g.setColor(Color.BLACK);
        //6 创建一个font 指定字体样式
        Font font=new Font(null,Font.BOLD,30);
        g.setFont(font);
        //7 选中字
        for (int i = 0; i <yzm.length() ; i++) {
            char  c=yzm.charAt(i);
            g.setColor(getColor());
            g.drawString(c+"",10+i*40,30);
        }
        //8 画线
        for (int i = 1; i <=10 ; i++) {
            //Color(int r, int g, int b)红色、绿色和蓝色所占成分，(0 - 255)
            g.setColor(getColor());
            g.drawLine((int)(Math.random()*85),
                    (int)(Math.random()*40),
                    (int)(Math.random()*85+85),
                    (int)(Math.random()*40));//随机x1 y1 x2 y2
        }
        //10 把内存中的图片的信息刷新到目的文件中
        ImageIO.write(bin,"JPEG",out);

    }
    //随机颜色
    private static  Color   getColor(){
            return new Color((int)(Math.random()*255),
                    (int)(Math.random()*255),
                    (int)(Math.random()*255));
    }

    //md5
    public static String EncoderByMd5(String str,String key) throws Exception {
        //确定算法
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        BASE64Encoder base64en = new BASE64Encoder();
        //加密后的字符串
        String newstr = base64en.encode(md5.digest((str+"key").getBytes("utf-8")));
        return newstr;
    }

}
